package code.lewdy.web.response;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import code.lewdy.util.DataType;
import code.lewdy.xml.XMLUtil;

public class ExtResponse
{
    
    /**Ajax返回操作成功结果
     * @param resp
     * @param msg
     * @param dataType
     * @return
     * @author liuding,2012-4-27
     */
    public static String ok(HttpServletResponse resp, String msg, String dataType){
        StringBuffer sb = new StringBuffer();
        String responseText = "";
        String contentType = DataType.XML;
        if(dataType.toUpperCase().indexOf("JSON")>=0){
            contentType = DataType.JSON;
        }else if(dataType.toUpperCase().indexOf("XML")>=0){
            contentType = DataType.XML;
            sb.append("<resp>");
            sb.append("<results>1</results>");
            sb.append("<message>"+msg+"</message>");
            sb.append("</resp>");
        }else if(dataType.toUpperCase().indexOf("HTML")>=0){
            contentType = DataType.HTML;
        }
        responseText = sb.toString();
        String encoding = "utf-8";
        resp.setContentType(contentType);   
        resp.setCharacterEncoding(encoding);
        resp.setHeader("Cache-Control", "no-cache");   
        PrintWriter out = null;
        try {
            out = resp.getWriter();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        out.print(responseText);
        out.close();
        return responseText;
    }
    
    /**Ajax返回操作失败结果
     * @param resp
     * @param msg
     * @param dataType
     * @return
     * @author liuding,2012-4-27
     */
    public static String error(HttpServletResponse resp, String msg, String dataType){
        StringBuffer sb = new StringBuffer();
        String responseText = "";
        String contentType = DataType.XML;
        if(dataType.toUpperCase().indexOf("JSON")>=0){
            contentType = DataType.JSON;
        }else if(dataType.toUpperCase().indexOf("XML")>=0){
            contentType = DataType.XML;
            sb.append("<resp>");
            sb.append("<results>-1</results>");
            sb.append("<message>"+msg+"</message>");
            sb.append("</resp>");
        }else if(dataType.toUpperCase().indexOf("HTML")>=0){
            contentType = DataType.HTML;
        }
        responseText = sb.toString();
        String encoding = "utf-8";
        resp.setContentType(contentType);   
        resp.setCharacterEncoding(encoding);
        resp.setHeader("Cache-Control", "no-cache");   
        PrintWriter out = null;
        try {
            out = resp.getWriter();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        out.print(responseText);
        out.close();
        return responseText;
    }
    
    /**Ajax输出字符串
     * @param resp
     * @param string
     * @param dataType
     * @return
     */
    public static String outString(HttpServletResponse resp, String string, String dataType){
    	String contentType = DataType.XML;
    	if(dataType.toUpperCase().indexOf("JSON")>=0){
            contentType = DataType.JSON;
        }else if(dataType.toUpperCase().indexOf("XML")>=0){
            contentType = DataType.XML;
        }else if(dataType.toUpperCase().indexOf("HTML")>=0){
            contentType = DataType.HTML;
        }
        String encoding = "utf-8";
        resp.setContentType(contentType);   
        resp.setCharacterEncoding(encoding);
        resp.setHeader("Cache-Control", "no-cache");   
        PrintWriter out = null;
        try {
            out = resp.getWriter();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        out.print(string);
        out.close();
    	return string;
    }
    
    /**Ajax方式返回list的数据
     * @param resp
     * @param list
     * @param dataType
     * @return
     * @author liuding,2012-4-27
     */
    public static String outDataList(HttpServletResponse resp, List<?> list, String dataType){
        String contentType = DataType.XML;
        String responseText = "";
        StringBuffer sb = new StringBuffer();
        if(dataType.toUpperCase().equals("JSON")){
            contentType = DataType.JSON;
        }else if(dataType.toUpperCase().equals("XML")){
            contentType = DataType.XML;
            sb.append("<resp>");
            sb.append("<size>"+list.size()+"</size>");
            sb.append(XMLUtil.listToXML(list));
            sb.append("</resp>");
        }else if(dataType.toUpperCase().equals("HTML")){
            contentType = DataType.HTML;
        }
        responseText = sb.toString();
        String encoding = "utf-8";
        resp.setContentType(contentType);   
        resp.setCharacterEncoding(encoding);
        resp.setHeader("Cache-Control", "no-cache");   
        PrintWriter out = null;
        try {
            out = resp.getWriter();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        out.print(responseText);
        out.close();
        return responseText;
    }
}
